package com.ai.bitcoin.service.helper;

import com.ai.bitcoin.jpa.dao.BccUserDao;
import com.ai.bitcoin.jpa.dao.BccUserWeixinGroupDao;
import com.ai.bitcoin.jpa.dao.BccUserWxinfoDao;
import com.ai.bitcoin.jpa.entity.BccUser;
import com.ai.bitcoin.jpa.entity.BccUserWeixinGroup;
import com.ai.bitcoin.jpa.entity.BccUserWxinfo;
import com.ai.bitcoin.vo.WeixinGroupVo;
import com.ai.bitcoin.weixin.template.BonusChangeTemplate;
import com.ai.bitcoin.weixin.template.SendReportService;
import com.ai.bitcoin.weixin.template.TemplateManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class UserServiceHelper {

    @Autowired
    private BccUserDao bitCoinWechatInfoDao;

    @Autowired
    private BccUserWxinfoDao bccUserWxinfoDao;

    @Autowired
    private BccUserWeixinGroupDao bccUserWeixinGroupDao;

    @Autowired
    BccUserDao bccUserDao;

    @Autowired
    SendReportService sendReportService;

    @Transactional
    public void updateUserBonus(Date date, Double bonus, String openId) {
        bitCoinWechatInfoDao.updateUserBonusByOpenId(date, bonus, openId);

    }

    @Transactional
    public void updateWxUser(BccUser user) {
        BccUserWxinfo userWxinfo = bccUserWxinfoDao.findByOpenId(user.getOpenId());
        if (user.getOpenId() != null && (user.getNickname() != null || user.getOpenId() != null)) {
            if (userWxinfo == null) {
                userWxinfo = new BccUserWxinfo();
                userWxinfo.setOpenId(user.getOpenId());
                userWxinfo.setImage(user.getImage());
                userWxinfo.setNickname(user.getNickname());
                bccUserWxinfoDao.save(userWxinfo);
            }
            else {
                bccUserWxinfoDao.updateWxinfo(user.getImage(), user.getNickname(), user.getOpenId());
            }
        }
    }

    @Transactional
    public boolean updateWxGroupUser(WeixinGroupVo vo, String openId) {

        BccUserWeixinGroup weixinGroup = bccUserWeixinGroupDao.findByGroupNameAndNickname(vo.getNickname(), vo.getGroupname());
        Date now = new Date();
        if (weixinGroup == null) {
            weixinGroup = new BccUserWeixinGroup();
            weixinGroup.setCreateTime(now);
            weixinGroup.setUpdateTime(now);
            weixinGroup.setGroupName(vo.getGroupname());
            weixinGroup.setNickname(vo.getNickname());
            weixinGroup.setGroupOwnerName(vo.getGroupOwnerName());
            weixinGroup.setOpenId(openId);
            bccUserWeixinGroupDao.save(weixinGroup);
        }
        else {
            log.info("关联微信群用户信息{} openId:{}", weixinGroup, openId);
            if (StringUtils.isNotEmpty(openId)) {
                BccUser srcUser = bccUserDao.findByOpenId(openId);
                weixinGroup.setOpenId(srcUser.getOpenId());
                weixinGroup.setUserId(srcUser.getId());
                bccUserWeixinGroupDao.save(weixinGroup);

                if (srcUser.getRelationTimes() >= 1) {
                    Map<String, Object> datazengsong = new HashMap<>();
                    datazengsong.put("openId", openId);
                    datazengsong.put("url", "http://explorelife.vip/bitcoin/bitcoin/media/" + openId + ".png");
                    datazengsong.put("first", "群用户关联公众号结果：曾关联过群用户无奖励");
                    datazengsong.put("FieldName", "变动原因");
                    datazengsong.put("Account", "群用户关联公众号");
                    datazengsong.put("change", "增加");
                    datazengsong.put("CreditChange", 0);
                    datazengsong.put("CreditTotal", srcUser.getBonus());
                    sendReportService.sendTemplateMsg(TemplateManager.buildTemplateParater(new BonusChangeTemplate(), datazengsong));
                }
                else if (StringUtils.isNotEmpty(vo.getUserId())) {
                    log.info("关联更新群用户币到公众号用户 {} -> openId:{}", openId, vo.getUserId());
                    BccUser toRemoveUser = bccUserDao.getOne(vo.getUserId());
                    srcUser.setBonus(toRemoveUser.getBonus() + srcUser.getBonus());
                    srcUser.setRelationTimes(srcUser.getRelationTimes() + 1);
                    srcUser = bccUserDao.save(srcUser);
                    bccUserDao.delete(toRemoveUser);

                    Map<String, Object> datazengsong = new HashMap<>();
                    datazengsong.put("openId", openId);
                    datazengsong.put("url", "http://explorelife.vip/bitcoin/bitcoin/media/" + openId + ".png");
                    datazengsong.put("first", "您的积分(PLC)变动如下");
                    datazengsong.put("FieldName", "增加原因");
                    datazengsong.put("Account", "群用户关联公众号");
                    datazengsong.put("change", "增加");
                    datazengsong.put("CreditChange", toRemoveUser.getBonus());
                    datazengsong.put("CreditTotal", srcUser.getBonus());
                    sendReportService.sendTemplateMsg(TemplateManager.buildTemplateParater(new BonusChangeTemplate(), datazengsong));
                }

            }
        }
        return true;

    }

}
